<?php

use yii\helpers\Html;
use yii\helpers;
//use yii\grid\GridView;
use kartik\grid\GridView;
use yii\widgets\Pjax;
use yii\grid\ActionColumn;
use backend\models\Country;
use common\grid\EnumColumn;
use kartik\grid\BooleanColumn;
use yii\widgets\ActiveForm;

/* @var $this yii\web\View */
/* @var $searchModel backend\models\Customersearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = '客户管理';
$this->params['breadcrumbs'][] = ['label' => '全部客户', 'url' => ['/customer/index']];
$this->params['breadcrumbs'][] = $this->title;

$js = <<<JS
$(function () {
  $('[data-toggle="tooltip"]').tooltip()
})
JS;
$this->registerJs($js);
?>

<div class="customer-index">
    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>
    <?php
    $columns = [
        [
            'class' => 'kartik\grid\CheckboxColumn',
            'headerOptions' => ['class' => 'kartik-sheet-style'],
        ],
        ['class' => 'yii\grid\SerialColumn'],
        /*        [
                    'attribute' => 'company',
                    'format' => 'raw',
                    'value' => function ($model) {
                        if (Yii::$app->user->id == $model->owner or $model->access() or Yii::$app->user->identity->superadmin == 1) {
                            return Html::a(mb_strcut($model->company, 0, 30), ['update', 'id' => $model->id]);
                        } else {
                            return mb_strcut($model->company, 0, 30);
                        }
                    }
                ],*/
        [
            'attribute' => 'company_province',
            'vAlign' => 'middle',
            'hAlign' => 'middle',
            'value' => function ($model) {
                return Country::findOne($model->company_province)->title;
            },
            'filterType' => GridView::FILTER_SELECT2,
            'filter' => Country::localCity('`pid`=0 AND `order`>0'),
            'filterWidgetOptions' => [
                'pluginOptions' => ['allowClear' => true],
            ],
            'filterInputOptions' => ['placeholder' => '选择省份'],
            'format' => 'raw',
        ],
        [
            'attribute' => 'company_city',
            'vAlign' => 'middle',
            'value' => function ($model) {
                if ($model->company_city) {
                    $modelCountry = new Country();
                    $company_city = implode(',', $modelCountry->getProvinces("id in ($model->company_city)"));
                } else {
                    $company_city = '';
                }
                return $company_city;
            },
            'filterType' => GridView::FILTER_SELECT2,
            'filter' => $searchModel->company_province ? Country::localCity("`pid`=" . $searchModel->company_province) : [],
            'filterWidgetOptions' => [
                'pluginOptions' => ['allowClear' => true],
            ],
            'filterInputOptions' => ['placeholder' => '选择城市'],
            'format' => 'raw'

        ],
        [
            'attribute' => 'company_type',
            'class' => 'common\grid\EnumColumn',
            'enum' => Yii::$app->params['company_type'],
        ],

        [
            'attribute' => 'company_level',
            'class' => 'common\grid\EnumColumn',
            'format' => 'raw',
            'enum' => Yii::$app->params['company_level_label'],
            'value' => function ($model) {
                return "<span title='" . Yii::$app->params['company_level'][$model->company_level] . "' data-toggle='tooltip' data-placement='top'>" . Yii::$app->params['company_level_label'][$model->company_level] . "</span>";
            }
        ],
        [
            'attribute' => 'type_desc',
            'class' => 'common\grid\EnumColumn',
            'enum' => Yii::$app->params['type_desc'],
            'format' => 'raw',
            'value' => function ($model) {
                $arr_type_desc = explode(',', $model->type_desc);
                $arr_customer_type_desc = Yii::$app->params['type_desc'];
                $arr_customer_type_desc_tag = [];

                if (is_array($arr_type_desc) && count($arr_type_desc) > 0) {
                    foreach ($arr_type_desc as $item) {
                        $arr_customer_type_desc_tag[] = $arr_customer_type_desc[$item];
                    }
                    $str_type_desc = implode(',', $arr_customer_type_desc_tag);
                    return "<span title='" . $str_type_desc . "' data-toggle='tooltip' data-placement='top'>" . mb_strcut($str_type_desc, 0, 30) . "</span>";
                } else {
                    return '';
                }

            }
        ],
        [
            'attribute' => 'province',
            'class' => 'common\grid\EnumColumn',
            'enum' => Country::localCity('pid = 0'),
            'format' => 'raw',
            'value' => function ($model) {
                $arr_province = explode(',', $model->province);
                $arr_province_tag = [];
                if (is_array($arr_province) && count($arr_province) > 0) {
                    foreach ($arr_province as $item) {
                        $arr_province_tag[] = Country::findOne(['id' => $item])->title;
                    }
                    return "<span title='" . implode(',', $arr_province_tag) . "' data-toggle='tooltip' data-placement='top'>" . mb_strcut(implode(',', $arr_province_tag), 0, 30) . "</span>";
                } else {
                    return '';
                }
            }
        ],
//            'principal',
//            [
//                'attribute' => 'package_type',
//                'class' => 'common\grid\EnumColumn',
//                'enum' => Yii::$app->params['package_type'],
//            ],
//            [
//                'attribute' => 'project_province',
//                'class' => 'common\grid\EnumColumn',
//                'enum' => Country::localCity('pid = 0'),
//                'format' => 'raw',
//                'value' => function ($model) {
//                    $arr_province = explode(',', $model->project_province);
//                    $arr_province_tag = [];
//                    if (is_array($arr_province) && count($arr_province) > 0) {
//                        foreach ($arr_province as $item) {
//                            $arr_province_tag[] = Country::findOne(['id' => $item])->title;
//                        }
//                        return "<span title='" . implode(',', $arr_province_tag) . "' data-toggle='tooltip' data-placement='top'>" . mb_strcut(implode(',', $arr_province_tag), 0, 30) . "</span>";
//                    } else {
//                        return '';
//                    }
//                }
//            ],
//            [
//                'attribute' => 'project_file',
//                'format' => 'raw',
//                'value' => function ($model) {
//                    if ($model->project_file) {
//                        $arr_file = explode('#', $model->project_file);
//                        return Html::a(mb_strcut($arr_file[1], 0, 15), $model->project_file);
//                    } else {
//                        return '';
//                    }
//                }
//            ],
//            [
//                'attribute' => 'package_transfer',
//                'class' => 'common\grid\EnumColumn',
//                'enum' => Yii::$app->params['package_transfer'],
//            ],
//            'investment:ntext',
//            'volume:ntext',
        [
            'attribute' => 'url',
            'format' => 'raw',
            'value' => function ($model) {
                return $model->url ? Html::a('网搜链接', $model->url, ['target' => '_blank']) : '';
            }
        ],

//            'created_at:date',
//            'updated_at:relativeTime',
//            [
//                'attribute' => 'review_level',
//                'value' => function ($model) {
//                    return Yii::$app->params['review_level'][$model->review_level];
//                }
//            ],
//            'first_mark_A',
//            [
//                'attribute' => 'review_level',
//                'class' => 'common\grid\EnumColumn',
//                'enum' => Yii::$app->params['review_level'],
//            ],
//            [
//                'attribute' => 'created_at',
//                'value' => function ($model) {
//                    return date('Y-m-d', strtotime($model->created_at));
//                }
//            ],
//            'is_need_clear',
        [
            'class' => 'kartik\grid\BooleanColumn',
            'attribute' => 'is_need_clear',
            'vAlign' => 'middle',
            'trueLabel' => '是',
            'falseLabel' => '否',
            'trueIcon' => '是',
            'falseIcon' => '否',
        ],
        [
            'attribute' => 'tag',
            'format' => 'raw',
            'value' => function ($model) {
                $arr_tag = explode(',', $model->tag);
                $arr_tag_tag = [];
                if (is_array($arr_tag) && count($arr_tag) > 0) {
                    foreach ($arr_tag as $item) {
                        $arr_tag_tag[] = \backend\models\Tags::findOne(['id' => $item])->title;
                    }
//                        return "<span title='" . implode(',', $arr_tag_tag) . "' data-toggle='tooltip' data-placement='top'>" . mb_strcut(implode(',', $arr_tag_tag), 0, 30) . "</span>";
                    return implode(',', $arr_tag_tag);
                } else {
                    return;
                }
            },
            'filterType' => GridView::FILTER_SELECT2,
            'filter' => \backend\models\Tags::getAllTags(),
            'filterWidgetOptions' => [
                'pluginOptions' => ['allowClear' => true, 'multiple' => false],
            ],
            'filterInputOptions' => ['placeholder' => '选择标签'],
        ],
        [
            'attribute' => 'need',
            'format' => 'raw',
            'value' => function ($model) {
                return "<span title='" . $model->need . "' data-toggle='tooltip' data-placement='top'>" . mb_strcut($model->need, 0, 30) . "</span>";
            }
        ],
        [
            'attribute' => 'market_area',
            'format' => 'raw',
            'value' => function ($model) {
                return "<span title='" . $model->market_area . "' data-toggle='tooltip' data-placement='top'>" . mb_strcut($model->market_area, 0, 30) . "</span>";
            }
        ],
        [
            'attribute' => 'owner',
            'value' => function ($model) {
                return $model->backendUser->realname;
            },
            'filterType' => GridView::FILTER_SELECT2,
            'filter' => \backend\models\User::getActiveUsers(),
            'filterWidgetOptions' => [
                'pluginOptions' => ['allowClear' => true],
            ],
            'filterInputOptions' => ['placeholder' => '创建人'],
            'format' => 'raw',
        ],
        [
            'attribute' => 'updated_at',
            'value' => function ($model) {
                return date('Y-m-d', strtotime($model->updated_at));
            }
        ],
        ['class' => 'yii\grid\ActionColumn', 'template' => '{view} {delete}',
            'buttons' => [
                'view' => function ($url, $model, $key) {
                    if (Yii::$app->user->id == $model->owner or $model->access() or Yii::$app->user->identity->superadmin == 1) {
                        return Html::a('查看', ['update', 'id' => $model->id]);
                    } else {
                        return;
                    }
                },

                'delete' => function ($url, $model, $key) {
                    return $model->owner == Yii::$app->user->id ? Html::a('删除', $url, [
                        'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'),
                        'data-method' => 'post',
                    ]) : '';
                },

            ],
        ],
    ];
    //    Pjax::begin();
    echo GridView::widget([
        'id' => 'kv-grid-customer',
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'options' => ['class' => 'responsive'],
        'condensed' => true,
        'pjax' => false, // pjax is set to always true for this demo
        'resizableColumns' => true,
        'persistResize' => false,
        'bordered' => true,
//        'perfectScrollbar' => true,
        'floatHeader' => false,
//        'floatHeaderOptions'=>['scrollingTop'=>'0'],
        'toolbar' => [
            [
                'content' =>
                    Html::a('<i class="glyphicon glyphicon-plus"></i>添加客户', ['create'], ['title' => '添加客户', 'class' => 'btn btn-success']) .
                    Html::a('<i class="glyphicon glyphicon-repeat"></i>重置', ['index'], ['class' => 'btn btn-default', 'title' => '重置列表'])
            ],
//            '{export}',
            '{toggleData}'
        ],
        'panel' => [
            'heading' => '<h3 class="panel-title"><i class="glyphicon glyphicon-book"></i> 客户信息</h3>',
            'type' => 'success',
            'footer' => '',
            'after' => '<div class="pull-right"><a class="btn btn-primary" data-toggle="modal" data-target="#sendMsg" 
onclick="javascript:var checked_user=$(\'#kv-grid-customer\').yiiGridView(\'getSelectedRows\');$(\'#sendmsg-mobile\').val(checked_user);"><i class="glyphicon glyphicon-send"></i> 消息推送</a></div><div class="clearfix"></div>',
        ],
        'columns' => $columns,
    ]);
    //    Pjax::end();
    ?>
</div>

<div class="modal fade" id="sendMsg" tabindex="-1" role="dialog"
     aria-labelledby="sendMsgLabel"
     aria-hidden="true">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="sendMsgLabel">消息推送</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <?php $form = ActiveForm::begin(['action' => ['/customer/send-msg'], 'method' => 'post',]) ?>
            <div class="modal-body">
                <?= $form->field($msgModel, 'mobile')->hiddenInput(['value' => ''])->label(false) ?>
                <?= $form->field($msgModel, 'content')->textarea(['placeholder' => '请输入要发送的内容。。。'])->label(false) ?>
            </div>
            <div class="modal-footer">
                <button type="submit" class="btn btn-primary">提交</button>
                <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
                <?= Html::a('推送记录', ['send-msg/index'], ['class' => 'btn btn-info', 'target' => '_blank']) ?>
            </div>
            <?php ActiveForm::end() ?>
        </div>
    </div>
</div>